package com.demo.shardingsphere.order.config;


import com.demo.shardingsphere.util.OrderUtil;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * 分库路由
 */
public class OrderDatabaseAlgorithm implements PreciseShardingAlgorithm<Long> {


    @Override
    public String doSharding(Collection<String> availableTargetNames,
                             PreciseShardingValue<Long> shardingValue) {
        Long userId = shardingValue.getValue();
        Integer uidSuffix= OrderUtil.getUidSuffixByUserId(userId);
        //分2个库
        String dbName= "db"+uidSuffix/2%2;
        return dbName;

    }
}
